package cd.bitmap;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.util.Arrays;

public class BitmapUtil {

    public static Bitmap binImageToBitmap(byte[] Input, int offset, int iImageX, int iImageY){
//    	final byte COLOR_WHITE = 0;
//    	final byte COLOR_BLACK = 1;
//    	final byte COLOR_R     = 2;
//    	final byte COLOR_RG    = 3;
//    	final byte COLOR_G     = 4;
//    	final byte COLOR_GB    = 5;
//    	final byte COLOR_B     = 6;
//    	final byte COLOR_BR    = 7;
//    	final byte COLOR_BR2   = 8;
//    	final byte COLOR_HUE   = (byte)255;
        byte[] w_bTemp = new byte[4];
        byte[] head = new byte[1078];
        final byte[] head2={
                /***************************/
                0x42,0x4d,0x0,0x0,0x0,0x00,0x00,0x00,0x00,0x00,
                0x36,0x4,0x00,0x00,


                0x28,0x00,0x00,0x00,
                0x00,0x00,0x0,0x00,
                0x00,0x00,0x00,0x00,

                0x01,0x00,
                0x08,0x00,
                0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,
                0x00,0x00,0x00,0x00,
        };


        int		i,j, num, iImageStep;

        Arrays.fill(w_bTemp, (byte)0);

        System.arraycopy(head2, 0, head, 0, head2.length);

        if ((iImageX % 4) != 0)
            iImageStep = iImageX + (4 - (iImageX % 4));
        else
            iImageStep = iImageX;

        int nSize = 1078+iImageStep*iImageY;

        head[2] = (byte) ((nSize) & 0xff);
        head[3] = (byte) ((nSize >> 8) & 0xff);
        head[4] = (byte) ((nSize >> 16) & 0xff);
        head[5] = (byte) ((nSize >> 24) & 0xff);

        byte[] Output = new byte[nSize];

        num=iImageX; head[18]= (byte)(num & (byte)0xFF);
        num=num>>8;  head[19]= (byte)(num & (byte)0xFF);
        num=num>>8;  head[20]= (byte)(num & (byte)0xFF);
        num=num>>8;  head[21]= (byte)(num & (byte)0xFF);

        num=iImageY; head[22]= (byte)(num & (byte)0xFF);
        num=num>>8;  head[23]= (byte)(num & (byte)0xFF);
        num=num>>8;  head[24]= (byte)(num & (byte)0xFF);
        num=num>>8;  head[25]= (byte)(num & (byte)0xFF);


        int dataSize = iImageStep*iImageY;
        head[34] = (byte) ((dataSize) & 0xff);
        head[35] = (byte) ((dataSize >> 8) & 0xff);
        head[36] = (byte) ((dataSize >> 16) & 0xff);
        head[37] = (byte) ((dataSize >> 24) & 0xff);

        j=0;
        for (i=54;i<1078;i=i+4)
        {
            head[i]=head[i+1]=head[i+2]=(byte)j;
            head[i+3]=0;
            j++;
        }

//    	head[54+2+4*COLOR_BLACK]= 0;
//    	head[54+1+4*COLOR_BLACK] = 0;
//    	head[54+COLOR_BLACK] = 0;
//    	head[54+3+4*COLOR_BLACK] = 0;
//
//    	head[54+2+4*COLOR_WHITE] = (byte) 210;
//    	head[54+1+4*COLOR_WHITE] = (byte) 210;
//    	head[54+4*COLOR_WHITE] = (byte) 210;
//    	head[54+3+4*COLOR_WHITE] = 0;
//
//    	head[54+2+4*COLOR_R] = (byte) 255;
//    	head[54+1+4*COLOR_R] = 0;
//    	head[54+4*COLOR_R] = 0;
//    	head[54+3+4*COLOR_R] = 0;
//
//    	head[54+2+4*COLOR_RG] = (byte) 255;
//    	head[54+1+4*COLOR_RG] = (byte) 255;
//    	head[54+4*COLOR_RG] = 0;
//    	head[54+3+4*COLOR_RG] = 0;
//
//    	head[54+2+4*COLOR_G] = 0;
//    	head[54+1+4*COLOR_G] = (byte) 255;
//    	head[54+4*COLOR_G] = 0;
//    	head[54+3+4*COLOR_G] = 0;
//
//    	head[54+2+4*COLOR_GB] = 0;
//    	head[54+1+4*COLOR_GB] = (byte) 255;
//    	head[54+4*COLOR_GB] = (byte) 255;
//    	head[54+3+4*COLOR_GB] = 0;
//
//    	head[54+2+4*COLOR_B] = 0;
//    	head[54+1+4*COLOR_B] = 0;
//    	head[54+4*COLOR_B] = (byte) 255;
//    	head[54+3+4*COLOR_B] = 0;
//
//    	head[54+2+4*COLOR_BR] = (byte) 255;
//    	head[54+1+4*COLOR_BR] = 0;
//    	head[54+4*COLOR_BR] = (byte) 255;
//    	head[54+3+4*COLOR_BR] = 0;
//
//    	head[54+2+4*COLOR_BR2] = (byte) 255;
//    	head[54+1+4*COLOR_BR2] = 0;
//    	head[54+4*COLOR_BR2] = (byte) 128;
//    	head[54+3+4*COLOR_BR2] = 0;
//
//    	head[54+2+4*COLOR_HUE] = (byte) 128;
//    	head[54+1+4*COLOR_HUE] = (byte) 128;
//    	head[54+4*COLOR_HUE] = (byte) 128;
//    	head[54+3+4*COLOR_HUE] = 0;

        System.arraycopy(head, 0, Output, 0, 1078);

        if (iImageStep == iImageX){
            for( i = 0; i < iImageY; i ++){
                System.arraycopy(Input, offset+i*iImageX, Output, 1078+i*iImageX, iImageX);
            }
        }
        else{
            iImageStep = iImageStep - iImageX;

            for( i = 0; i < iImageY; i ++){
                System.arraycopy(Input, offset+i*iImageX, Output, 1078+i*(iImageX+iImageStep), iImageX);
                System.arraycopy(w_bTemp, 0, Output, 1078+i*(iImageX+iImageStep)+iImageX, iImageStep);
            }
        }

        Bitmap image = BitmapFactory.decodeByteArray(Output, 0, nSize);
        return image;
    }
}
